package com.example.immanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.immanager.common.Result;
import com.example.immanager.entity.Im;
import com.example.immanager.service.ImService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lukecheng
 * @date 2023/04/19
 */


@RestController
@RequestMapping("/im")
public class ImController {

    @Resource
    private ImService imService;

    @GetMapping("/init/{limit}")
    public Result findAllInit(@PathVariable Integer limit) {
        List<Im> ims = imService.list(new QueryWrapper<Im>().orderByDesc("id").last("limit " + limit));
        return Result.success(ims.stream().sorted(Comparator.comparing(Im::getId)).collect(Collectors.toList()));
    }


}
